local DEFAULTSTRENGTH = 16

freeslot("MT_EGGMANBUMPER", "S_EGGMANBUMPER", "S_EGGMANBUMPERHIT", "SPR_BUME", "sfx_egtrap")

mobjinfo[MT_EGGMANBUMPER] = {
	//$Sprite BUMEA0
	//$Name Eggman Bumper
	//$Flags4Text Strength depends on Player's speed
	//$Category Springs and Fans
	//$AngleText Strength
	spawnstate = S_EGGMANBUMPER,
	radius = 32*FRACUNIT,
	height = 64*FRACUNIT,
	doomednum = 461,
	dispoffset = 0,
	deathstate = S_NULL,
	painsound = sfx_s3kaa,
	flags = MF_SPECIAL|MF_NOGRAVITY
}

states[S_EGGMANBUMPER] = {SPR_BUME, A, -1, nil, 0, 0, S_EGGMANBUMPER}
states[S_EGGMANBUMPERHIT] = {SPR_BUME, FF_ANIMATE|D, 12, nil, 1, 3, S_EGGMANBUMPER}

local function bumpPlayer(bumpermo, playermo)
	--Determine the angle the player hit the bumper from.
	local horizontalAngle = R_PointToAngle2(bumpermo.x,bumpermo.y,playermo.x,playermo.y)
	local verticalAngle = R_PointToAngle2(FixedHypot(bumpermo.x,bumpermo.y),bumpermo.z+(bumpermo.height/2),FixedHypot(playermo.x,playermo.y),playermo.z+(playermo.height/2))
	
	-- This sets whether or not the player is holding the jump button.
	-- Without this line, the player can let go of the jump button to reduce the bumper's effects.
	playermo.player.jumping = false
	
	-- If the bumper was created by another object or script, the minimum strength will be determined by the DEFAULTSTRENGTH value at the top of this script.	
	local bumperStrength = DEFAULTSTRENGTH
	
	-- If the bumper was placed on the map in ZoneBuilder, the angle will determine the bumper's minimum strength.
	if bumpermo.spawnpoint and bumpermo.spawnpoint.valid and bumpermo.spawnpoint.angle > 0
		bumperStrength = bumpermo.spawnpoint.angle/10
	end
	
	-- Bounce the player back slightly slower than the speed at which they hit the bumper (Special Flag).
	if (bumpermo.spawnpoint and bumpermo.spawnpoint.valid and bumpermo.spawnpoint.options & MTF_OBJECTSPECIAL)
		local playerVelocity = FixedHypot(playermo.player.speed, playermo.momz)
		if bumperStrength < (9*playerVelocity) / (10*FRACUNIT)
			bumperStrength = (9*playerVelocity) / (10*FRACUNIT)
		end
	end
	
	-- Bounce the player back at the angle they hit the bumper from.
	P_SetObjectMomZ(playermo, bumperStrength*sin(verticalAngle), false)
	P_InstaThrust(playermo, horizontalAngle, bumperStrength*abs(cos(verticalAngle)))
	playermo.eggstun = 2*TICRATE
	playermo.eggstun_bounce = 0
	
	-- Show the "hit" frames and play the bumper sound (but only if the bumper is not already being hit).
	if bumpermo.state == S_EGGMANBUMPER
		S_StartSound(bumpermo, bumpermo.info.painsound)
		S_StartSound(bumpermo, sfx_egtrap)
		bumpermo.state = S_EGGMANBUMPERHIT
	end
	return true
end

-- This function keeps all bumpers synced up to the same frames.
-- If you want to change the bumper's animation, here is where you do it.
local function syncAnimation(bumpermo)
	if bumpermo.state == S_EGGMANBUMPER
		if leveltime % 16 < 5
			bumpermo.frame = 0
		elseif leveltime % 16 < 8
			bumpermo.frame = 1
		elseif leveltime % 16 < 13
			bumpermo.frame = 2
		else
			bumpermo.frame = 1
		end
	end
end

addHook("TouchSpecial",bumpPlayer,MT_EGGMANBUMPER)
addHook("MobjThinker", syncAnimation, MT_EGGMANBUMPER)

//Custom Stun
freeslot("S_PLAY_EGGSTUN")

states[S_PLAY_EGGSTUN] = {SPR_PLAY, SPR2_PAIN|FF_ANIMATE, -1, nil, 0, 4, S_PLAY_EGGSTUN}

addHook("PlayerThink", function(player)
	if player.mo
	and player.mo.valid
	and player.playerstate == PST_LIVE
		if player.mo.eggstun_bounce == nil
		player.mo.eggstun_bounce = 0
		end
		if player.mo.eggstun == nil //Custom Stun
		player.mo.eggstun = 0
		else
			if player.mo.eggstun > 0
			player.mo.eggstun = $1 - 1
				if player.mo.state != S_PLAY_EGGSTUN
				player.mo.state = S_PLAY_EGGSTUN
				end
			player.pflags = $1 & ~PF_APPLYAUTOBRAKE
			P_ResetPlayer(player)
			player.powers[pw_nocontrol] = 2
			player.drawangle = $1 + ANG20
			local stunjump
				if (player.mo.eflags & MFE_UNDERWATER)
				stunjump = 3*FRACUNIT
				else
				stunjump = 6*FRACUNIT
				end
				if P_IsObjectOnGround(player.mo)
				P_SetObjectMomZ(player.mo, stunjump)
					if player.speed < 4*FRACUNIT
					P_InstaThrust(player.mo, R_PointToAngle2(0, 0, player.mo.momx, player.mo.momy), 4*FRACUNIT)
					end
				player.mo.eggstun_bounce = $1 + 1
					if player.mo.eggstun_bounce >= 2
					player.mo.eggstun = 0
					end
				end
			else
			player.mo.eggstun_bounce = 0
				if player.mo.state == S_PLAY_EGGSTUN
					if P_IsObjectOnGround(player.mo)
						if player.speed > 0
						player.mo.state = S_PLAY_WALK
						else
						player.mo.state = S_PLAY_STND
						end
					else
					player.mo.state = S_PLAY_FALL
					end
				end
			end
		end
	end
end)

//Bug Fix
addHook("MobjDamage", function(mo)
mo.eggstun = 0
end, MT_PLAYER)